/**
 *
 */
package com.engine.Mangers;

import java.util.List;

import org.hibernate.Session;

import com.util.EngineUtil;
import com.wfe.db.SessionUtil;
import com.wfe.db.WorkFlowEngineQueryUtil;
import com.wfe.mapping.client.WfProjectStatus;
import com.wfe.mapping.client.WfProjectTask;
import com.wfe.mapping.client.WfProjectTaskStatus;
import com.wfe.mapping.client.WfTaskType;

/**
 * @author kareem_yehya
 * @since 18-August-2009
 * 
 * <br>
 *        This manger Responsible for Solving Those Automatic Tasks Problems <br>
 *        1- When Shutdown Taskmanger Engine There are many Tasks with inprocess status which will not be collected again.
 * 
 * 
 */

public class RepareAutoamticTasks implements Runnable
{

	/**
	 * @param args
	 */
	public static void main(String[] args)
	{
		(new Thread(new RepareAutoamticTasks())).start();

	}

	// @Override
	public void run()
	{

		WfProjectTaskStatus assignedStatus = getWfProjectTaskStatusByID(4l); // assigned
		while(true)
		{
			try
			{
				// ------------------------- assigned process tasks
				System.out.println("Start RepareAutoamticTasks");
				// TODO should change static number to parmaeter
				List<WfProjectTask> taskList = loadStopedProcessTasks(1000);

				if(taskList.size() > 0)
				{
					for(WfProjectTask projectTask : taskList)
					{//
						projectTask.setWfProjectTaskStatus(assignedStatus);// assigned
						updateTask(projectTask);
					}

				}

				Thread.sleep(1000);
			}catch(Exception e)
			{
				e.printStackTrace();
			}
		}

	}

	public List<WfProjectTask> loadStopedProcessTasks(int maxresults)
	{

		List<WfProjectTask> tasklist = null;
		Session session = null;
		try
		{
			WfTaskType taskType = getwfTaskTypeByTypeID(3); // automatic
			WfProjectTaskStatus inprocessStatus = getWfProjectTaskStatusByID(3l); // inprocess
			WfProjectStatus projectStatus = getWfProjectStatusByID(2); // inprocess
			session = SessionUtil.getSession();
			tasklist = WorkFlowEngineQueryUtil.loadStopedProcessTasks(session, projectStatus, maxresults, taskType, inprocessStatus);
			return tasklist;
		}catch(Exception e)
		{
			e.printStackTrace();

		}finally
		{
			EngineUtil.closeSession(session);

		}
		return null;

	}

	private void updateTask(WfProjectTask task)
	{
		Session session = null;
		try
		{
			session = SessionUtil.getSession();
			WorkFlowEngineQueryUtil.updateWfProjectTask(task, session);
		}catch(Exception e)
		{
			e.printStackTrace();
		}finally
		{
			EngineUtil.closeSession(session);

		}

	}

	public WfTaskType getwfTaskTypeByTypeID(long tasktype)
	{

		Session session = null;
		try
		{
			session = SessionUtil.getSession();
			WfTaskType taskType = WorkFlowEngineQueryUtil.getWfTaskType(session, tasktype);

			return taskType;

		}catch(Exception e)
		{
			e.printStackTrace();
		}finally
		{
			EngineUtil.closeSession(session);

		}
		return null;

	}

	private WfProjectTaskStatus getWfProjectTaskStatusByID(long i)
	{
		Session session = null;
		try
		{
			session = SessionUtil.getSession();
			WfProjectTaskStatus projectTaskStatus = WorkFlowEngineQueryUtil.getWfProjectTaskStatusByID(i, session);

			return projectTaskStatus;
		}catch(Exception e)
		{
			e.printStackTrace();
		}finally
		{
			EngineUtil.closeSession(session);

		}
		return null;

	}

	private WfProjectStatus getWfProjectStatusByID(long i)
	{
		Session session = null;
		try
		{
			session = SessionUtil.getSession();
			WfProjectStatus projectStatus = WorkFlowEngineQueryUtil.getwfProjectStatusByID(i, session);

			return projectStatus;
		}catch(Exception e)
		{
			e.printStackTrace();
		}finally
		{
			EngineUtil.closeSession(session);

		}
		return null;

	}

}
